package br.com.prime.model.bussines;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.prime.model.ConnectionFactory;
import br.com.prime.model.pojo.Professor;

public class ProfessorDAO {

	private Connection connection;

	public ProfessorDAO() {
		try {
			this.connection = new ConnectionFactory().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Professor> getAll() {
		List<Professor> professores = new ArrayList<Professor>();
		String sql = "SELECT * FROM PROFESSOR";
		try {
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			professores = montarObjeto(rs);
			rs.close();
			stmt.close();
			return professores;
		} catch (SQLException e) {
			e.printStackTrace();
			return professores;
		}
	}
	
	public List<Professor> montarObjeto(ResultSet rs){
		List<Professor> professores = new ArrayList<Professor>();
		try {			
			while (rs.next()) {
				Professor professor = new Professor();
				professor.setID_PROFESSOR(rs.getInt("iD_PROFESSOR"));
				professor.setNOME(rs.getString("NOME"));
				professor.setID_ENDERECO(rs.getInt("iD_ENDERECO"));
				professor.setID_MATERIA(rs.getInt("iD_MATERIA"));
				
				
				professores.add(professor);
			}	
			return professores;
		} catch (Exception e) {
			e.printStackTrace();
			return professores;
		}		
	}
}